﻿(function ($) {
      var defualts = {
            width: "150px",//宽度
            treeWidth: '150px',//树宽度
            panleHeight: "500px",//面板高度
            panleWidth: "400px",//面板宽度
            OnSelect: undefined, //选择事件
            singleSelect: true,//是否单选
            onChange: undefined,//选择事件
        };
       $.initCombox = function (t, gopts) {
            //console.info(t);console.info(gopts);
            var c={
                  griddata: undefined,//列表缓存
                  setflag: true,//是否更新列表缓存 
                  checkObjarr:[],
                  onChange: function (newValue, oldValue) {
                    if (t.p.onChange)
                        t.p.onChange(newValue, oldValue);
                    },
                  listClick: function (index, row) {//用户数据行选择
                    var arr = t.combo.checkObjarr;
                    if (gopts.singleSelect) {//单选
                        arr = [row];
                        t.combo.$e.combo("setText", row.NAME);
                        t.combo.$e.combo("setValue", row.ID);
                    } else {//多选
                        var check = t.combo.userObj.datalist("getChecked");
                        arr = check;
                        var ids = [];
                        var strname = '';
                        $.each(arr, function (i, n) {
                            ids.push(n.ID);
                            strname += (i == 0 ? n.NAME : "," + n.NAME);
                        });
                        t.combo.$e.combo("setValues", ids);
                        t.combo.$e.combo("setText", strname);
                    }
                    t.combo.checkObjarr = arr;
                  },
                  Init: function () {//初始化
                    this.$e = $(t);
                    this.$e.combo({
                        panelWidth: gopts.panleWidth,
                        panelHeight: gopts.panleHeight,
                        multiple: !gopts.singleSelect,
                        editable: false,
                        onChange: function (newValue, oldValue) {
                            t.combo.onChange(newValue, oldValue);
                        }
                    });
                    panle = this.$e.combo("panel");
                    var panleBody = $(panle[0]);
            
                var html = '<div class="easyui-layout" style="width:' + (parseInt(gopts.panleWidth.replace("px")) - 5) + 'px;height:' + (parseInt(gopts.panleHeight.replace("px")) - 5) + 'px;">';
                    html += " <div data-options=\"region:'west'\" style=\"width:" + gopts.treeWidth + ";\"><ul  id=\"orgTreeData\"></ul></div> ";
                    html += "<div data-options=\"region:'center'\" style=\"padding:0px\"><div id=\"userDataList\"></div></div></div>";
                    
                    panleBody.append(html);
                    $('.easyui-layout', panleBody).layout();
                    this.orgObj = $('#orgTreeData', panleBody);
                    this.userObj = $("#userDataList", panleBody);
                    
                  //初始化用户列表
                 this.userObj.datalist({
                          url:getRootPath() + "/Ashx/ZsHandler/ZSTransactionHandler.ashx?action=GetUserListData&ids=",
                          idField: 'ID',
                          valueField: "ID",
                          textField: 'NAME',
                          checkbox: !gopts.singleSelect,
                          singleSelect: gopts.singleSelect,
                          lines:true,
                          onSelect: function (index, row) {
                             t.combo.listClick(index, row);
                            },
                          onUncheck: function (index, row) {
                                t.combo.listClick(index, row);
                            },
                          onClickRow: function (index, row) {
                                if (t.p.singleSelect) {
                                    setTimeout(function () {
                                        t.combo.$e.combo("hidePanel");
                                    }, 200);
                                }
                            },
                          onCheck: function (index, row) {
                                if (!gopts.singleSelect) {
                                    t.combo.listClick(index, row);
                                }
                            },
                         onCheckAll: function (rows) {
                                if (!gopts.singleSelect) {
                                    $.each(rows, function (r, row) {
                                        t.combo.listClick(r, row);
                                    });
                                }
                            },
                         onUncheckAll: function (rows) {
                                if (!gopts.singleSelect) {
                                    $.each(rows, function (r, row) {
                                        t.combo.listClick(r, row);
                                    });
                                }
                            },
                         onLoadSuccess: function (data) {
                              //console.info(data);
                              if (t.combo.setflag) { t.combo.griddata = data; }
                               if (!gopts.singleSelect) {
                                  //t.combo.userObj.datagrid('uncheckAll');
//                                  window.setTimeout(function () {
//                                  var ids=t.combo.$e.combo("getValues");
//                                  console.info(ids);
//                                  if(ids.length>0){
//                                      $.each(data.rows,function(index,item){
//                                            if(ids.indexOf(item.ID)>=0){
//                                                t.combo.userObj.datagrid('checkRow',index);
//                                            }else{
//                                                t.combo.userObj.datagrid('uncheckRow',index);
//                                            }
//                                      });
//                                }},100)
                              }
                          }
                      });
                  //初始化组织树
                 this.orgObj.tree({
                        url: getRootPath() + "/Ashx/ZsHandler/ZSTransactionHandler.ashx?action=GetTreeData",
                        formatter: function (node) {
                            return node.text;
                        },
                        onSelect: this.orgTreeSelect,
                        lines: true,
                        fit: true,
                        onLoadSuccess: function (node, data) {
                        }
                    });
                    
                },
                  orgTreeSelect: function (node) {//树形菜单点击获取相应用户数据
                       var orgids = node.id;
                       if (node.children!=undefined&&node.children.length !== 0) {
                           orgids = ""+node.id+",";
                           $(node.children).each(function (index, item) {
                               orgids += this.id + ",";
                           });
                        }
                        $("#MAINUNIT").textbox('setText',node.text);
                        var tempopts = t.combo.userObj.datalist("options");
                        if (tempopts) {
                            tempopts.url = getRootPath() + "/Ashx/ZsHandler/ZSTransactionHandler.ashx?action=GetUserListData&ids="+orgids;
                            t.combo.userObj.datalist(tempopts);
                        }
                }
            }
             if (!t.combo) {
                    c.$e = $(t);
                    c.Init();
                    t.combo = c;
                    t.p = gopts;
                }
            return t;
        };
       //定义判断页面是否加载完成
       var docloaded = false;
       $(document).ready(function () { docloaded = true });
       $.fn.selectUserCombox = function (options) {
          var p = $.extend({}, defualts, options);
          var x = this.selector;
          $(x).usercomboxone(p);
      };
      $.fn.usercomboxone = function (options) {
          var p = $.extend({}, defualts, options);
          return this.each(function () {
            if (!docloaded) {//未完成，等待完成初始化弹窗
                var t = this;
                $(document).ready
                    (
                    function () {
                        $.initCombox(t, p);
                    }
                    );
            } else {//完成，直接初始化弹窗
                $.initCombox(this, p);
            }
        });
      };
  })(jQuery)